package cn.micai.base.lambda;

import java.util.HashMap;
import java.util.Map;

/**
 * 描述：Java 8 forEach examples forEach and Map
 * <p>
 *
 * @author: 赵新国
 * @date: 2018/6/21 14:51
 */
public class ForEachMapExample {

    public static void main(String [] args) {

        Map<String, Integer> items = new HashMap<>();

        items.put("A", 10);
        items.put("B", 20);
        items.put("C", 30);
        items.put("D", 40);
        items.put("E", 50);
        items.put("F", 60);

        // Normal way to loop a Map
        for (Map.Entry<String, Integer> entry : items.entrySet()) {
            System.out.println("Item: " + entry.getKey() + " Count: " + entry.getValue());
        }

        System.out.println("--------------------------------------------");

        // In Java 8, you can loop a Map with forEach + lambda expression.
        items.forEach((k, v)->System.out.println("Item: " + k + " Count: " + v));

        System.out.println("--------------------------------------------");

        items.forEach((k, v)->{
            System.out.println("Item: " + k + " Count: " + v);
            if ("E".equals(k)) {
                System.out.println("Hello E......");
            }
        });

    }


}
